<template>
  <el-row
    id="globalHeader"
    style="margin-bottom: 16px"
    align="center"
    :wrap="false"
  >
    <el-col :span="22">
      <el-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
        :router="true"
      >
        <el-menu-item key="0" :style="{ padding: 0, marginRight: '38px' }">
          <div class="titleBar">
            <!--            <img class="logo" src="../assets/wgbdd.png" alt="" />-->
            <img
              src="https://ts1.cn.mm.bing.net/th?id=ODLS.bdc80c6d-c313-413d-b861-b4676ea5c796&amp;w=32&amp;h=32&amp;qlt=90&amp;pcl=fffffa&amp;r=0&amp;o=6&amp;pid=1.2"
              height="32"
              width="32"
              alt="全球 Web 图标"
              class="rms_img"
              data-bm="25"
            />
            <div class="title">猫眼电影</div>
          </div>
        </el-menu-item>
        <el-menu-item
          v-for="item in visiableRoutes"
          :key="item.path"
          :index="item.path"
        >
          {{ item.name }}
        </el-menu-item>
      </el-menu>
    </el-col>
    <el-col :span="2">
      <div v-if="loginUserStore.loginUser.id" class="container">
        <el-dropdown>
          {{ loginUserStore.loginUser.userName ?? "无名" }}
          >
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="tuiChu">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div v-else class="container">
        <el-button
          type="primary"
          tag="a"
          href="/user/login"
          rel="noopener noreferrer"
        >
          Login
        </el-button>
      </div>
    </el-col>
  </el-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/router";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useLoginUserStore } from "@/store/userStore";
import checkAccess from "@/access/checkAccess";

const loginUserStore = useLoginUserStore();
const router = useRouter();
const selectedKeys = ref(["/"]);

const visiableRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    //根据权限判断
    if (!checkAccess(loginUserStore.loginUser, item.meta?.access as string)) {
      return false;
    }
    return true;
  });
});

router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

const tuiChu = () => {
  loginUserStore.logout();
  router.push({ path: "/user/login" });
};
</script>

<style scoped>
#globalHeader {
  .container {
    margin-top: 24px;
    margin-left: 6px;
  }

  .titleBar {
    display: flex;
    align-items: center;
  }

  .title {
    margin-left: 16px;
    color: black;
  }

  .logo {
    height: 48px;
  }
}
</style>
